﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using MS.InformationManagement.Presenter.ResponseVoBo;
using MS.InformationManagement.Support;
using System.Web;
using MS.InformationManagement.Support.Constants;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using MS.InformationManagement.Presenter.Support;

namespace MS.InformationManagement.WP.Layouts.MS.InformationManagement.WP
{
    public partial class ResponseForVoBo : BaseApplicationPage, IResponseVoBoView
    {

        #region fields

        ResponseVoBoPresenter _presenter;

        #endregion

        #region Properties

        public bool Succed
        {
            get { return string.IsNullOrEmpty(Page.Request.QueryString["succed"])? false : true; }
        }

        public string Observacion
        {
            get { return txtObservacion.Text; }
        }

        public bool Approval
        {
            get { return ddlOptions.SelectedValue == "1"; }
        }

        public DAL.DocumentCenter.Archivo ArchiveEntity { get; set; }

        public int SelectedId { get { return int.Parse(Page.Request.QueryString["ID"]); } }

        #endregion

        #region Form Events

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            txtObservacion.Required(ExceptionConstants.ERROR_CAMPO_NULO, AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            //txtObservacion.RegularExpression(RegularExpresions.EXP_MAXLENGTH.Replace("#", "1000"), string.Format(ExceptionConstants.ERROR_MAX_LENGTH, "1000"), AjaxControlToolkit.ValidatorCalloutPosition.BottomRight);
            //this.AddRibbonActions();
        }

        protected void Page_Load(object sender, EventArgs e) 
        {
            try
            {
                if (Succed)
                {
                    lnkFile.Visible = false;
                    WebControl[] controls = new WebControl[] { this.txtObservacion, this.ddlOptions, this.btnCancelar, this.btnGuardar };
                    HandleSuccessRedirect("Respuesta enviada exitosamente", controls, true);
                }
                else
                {
                    _presenter = new ResponseVoBoPresenter(this);
                    if (IsPostBack) return;
                    _presenter.HandleLoad();
                }
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Save(object sender, EventArgs e)
        {
            try
            {
                _presenter.SaveResponse();
                lnkFile.HRef = "javascript:void(0);";
                btnCancelar.Text = "Cerrar";
                HandleSuccessRedirect("Respuesta enviada exitosamente", new WebControl[] { btnGuardar, txtObservacion, ddlOptions }, true, false);
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Action_Cancel(object sender, EventArgs e)
        {
            this.CloseWindow(false);
        }

        #endregion

        #region Implemented Methods

        public void Show()
        {
            lblSolicitudDesc.Text = ArchiveEntity.Solicitud;
            lnkFile.HRef = string.Format("javascript:editDocumentWithProgID2('{0}{1}', '', 'SharePoint.OpenDocuments', '0', '{0}', '0');", 
                SPContext.Current.Web.Url, ArchiveEntity.DocumentUrl.Replace("/sites/docs", string.Empty));
            lblNombre.Text = ArchiveEntity.Name;
            lblUserName.Text = PeopleHelper.GetUserById(ArchiveEntity.Author.LookupId).Name;
        }

        public void Success()
        {
            Response.Redirect("/Sites/Docs/_layouts/MS.InformationManagement.WP/ResponseForVoBo.aspx?succed=1");
        }

        #endregion

    }
}
